package cn.ezios.huo.system.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ezios.huo.system.dao.domain.SysRoleMenu;
import cn.ezios.huo.system.dao.service.SysRoleMenuService;
import cn.ezios.huo.system.dao.mapper.SysRoleMenuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 王坚
 * @description 针对表【sys_role_menu(系统角色菜单)】的数据库操作Service实现
 * @createDate 2024-07-17 09:49:16
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
        implements SysRoleMenuService {

    /**
     * 创建角色菜单关联关系
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createRoleMenu(String roleId, List<String> menuIds) {
        List<SysRoleMenu> preStoreList = menuIds.stream().map(menuId -> new SysRoleMenu(roleId, menuId)).toList();
        return saveBatch(preStoreList);
    }

    /**
     * 删除角色菜单关联关系
     *
     * @param roleId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRoleMenu(String roleId) {
//        LambdaQueryChainWrapper<SysRoleMenu> wrapper2 = lambdaQuery().eq(SysRoleMenu::getRoleId, roleId);
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId, roleId);
        return remove(wrapper);
    }

    /**
     * 根据角色id查询所有菜单
     *
     * @param roleId
     * @return
     */
    @Override
    public List<String> getRoleMenuIds(String roleId) {
        List<String> menuIds = lambdaQuery()
                .select(SysRoleMenu::getMenuId)
                .eq(SysRoleMenu::getRoleId, roleId)
                .list()
                .stream().map(SysRoleMenu::getMenuId)
                .toList();
        return menuIds;
    }

    /**
     * 根据角色id查询所有叶子菜单，为前端树形菜单做准备
     *
     * @param roleId
     * @return
     */
    @Override
    public List<String> getRoleMenuLeafIds(String roleId) {
        return getBaseMapper().getRoleMenuLeafIds(roleId);
    }

    /**
     * 根据输入的几个角色id查询所有对应的菜单并集
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<String> getMenuIdsByRoleIds(List<String> roleIds) {
        //SELECT DISTINCT menu_id FROM `sys_role_menu` WHERE role_id in ('1836973836446490626','1');
//        return lambdaQuery()
//                .select(SysRoleMenu::getMenuId)
//                .in(SysRoleMenu::getRoleId, roleIds)
//                .list()
//                .stream().map(SysRoleMenu::getMenuId)
//                .distinct()
//                .toList();
        if(roleIds.isEmpty()){
            return List.of();
        }
        return getBaseMapper().getMenuIdsByRoleIds(roleIds);
    }
}




